package com.youfan.control;

import com.alibaba.fastjson.JSONObject;
import com.youfan.entity.EsPage;
import com.youfan.entity.SearchInfo;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.utils.ElasticsearchUtil;
import io.swagger.annotations.ApiParam;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("search")
public class SearchOutControl {

    @RequestMapping(value = "createIndex")
    public boolean createIndex(@RequestBody SearchInfo searchInfo) {
        String index = searchInfo.getIndex();
        return ElasticsearchUtil.createIndex(index);
    }

    @RequestMapping(value = "deleteIndex")
    public boolean deleteIndex(@RequestBody SearchInfo searchInfo) {
        String index = searchInfo.getIndex();
        return ElasticsearchUtil.deleteIndex(index);
    }

    @RequestMapping(value = "listIndex")
    @ResponseBody
    public Result listIndex(@ApiParam(value = "索引查询条件", required = false) @RequestBody SearchInfo searchInfo) {
        try {
            if (searchInfo.getPageNum() == null) {
                searchInfo.setPageNum(1);
            }
            if (searchInfo.getPageSize() == null) {
                searchInfo.setPageSize(5);
            }
            CommonPage<SearchInfo> resultdata = ElasticsearchUtil.listIndex(searchInfo);

            return Result.success(resultdata, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询失败" + e.getMessage());
        }
    }

    @RequestMapping(value = "addData",method = RequestMethod.POST)
    public String addData( @RequestBody SearchInfo searchInfo) {

        String index = searchInfo.getIndex();
        String type = searchInfo.getType();
        String id = searchInfo.getId();
        String data = searchInfo.getData();
        return ElasticsearchUtil.addData(JSONObject.parseObject(data),index,type,id);
    }

    @RequestMapping(value = "deleteDataById")
    public void deleteDataById(@RequestBody JSONObject jsonObject) {
        String index = jsonObject.getString("index");
        String type = jsonObject.getString("type");
        String id = jsonObject.getString("id");
        ElasticsearchUtil.deleteDataById(index,type,id);
    }

    @RequestMapping(value = "updateDataById",method = RequestMethod.POST)
    public  void updateDataById(@RequestBody  JSONObject jsonObject) {
        String index = jsonObject.getString("index");
        String type = jsonObject.getString("type");
        String id = jsonObject.getString("id");
        ElasticsearchUtil.updateDataById(jsonObject,index,type,id);
    }

    @RequestMapping(value = "searchData",method = RequestMethod.POST)
    public   Result searchData(@RequestBody SearchInfo searchInfo) {
        try {
            if (searchInfo.getPageNum() == null) {
                searchInfo.setPageNum(1);
            }
            if (searchInfo.getPageSize() == null || searchInfo.getPageSize() ==  0 ) {
                searchInfo.setPageSize(5);
            }
            CommonPage<SearchInfo> resultdata = ElasticsearchUtil.searchData( searchInfo);

            return Result.success(resultdata, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询失败" + e.getMessage());
        }
    }

    @RequestMapping(value = "searchDataPage",method = RequestMethod.POST)
    public EsPage searchDataPage(@RequestParam String index, @RequestParam String type, @RequestParam int startPage, @RequestParam int pageSize, @RequestParam boolean matchPhrase, @RequestParam String searchField, @RequestParam(required =  false)  String sortField, @RequestParam(required =  false)  String highlightField, @RequestParam String searchword, @RequestParam(required =  false)  String viewsField) {
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            if (matchPhrase == Boolean.TRUE) {
                //不进行分词搜索
                boolQuery.must(QueryBuilders.matchPhraseQuery(searchField, searchword));
            } else {
                boolQuery.must(QueryBuilders.matchQuery(searchField, searchword));
            }
            EsPage esPage = ElasticsearchUtil.searchDataPage(index, type, startPage, pageSize,boolQuery, viewsField, sortField, highlightField);
            return esPage;
    }

}
